package com.example.messagewall.controller;

import com.example.messagewall.model.MessageInfo;
import com.example.messagewall.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private MessageService messageService;
    @RequestMapping("/publish")
    public Boolean publish(MessageInfo messageInfo) {
        //1.参数校验
        //2.存储数据
        System.out.println(messageInfo.toString());
        if(!StringUtils.hasLength(messageInfo.getFrom()) ||
                !StringUtils.hasLength(messageInfo.getTo()) ||
                !StringUtils.hasLength(messageInfo.getMessage())) {
            return false;
        }

        System.out.println(messageInfo);
        Integer result = messageService.publishMessage(messageInfo);
        if(result > 0) return true;
        return false;
    }

    @RequestMapping("/getList")
    public List<MessageInfo> getList() {
        return messageService.getMessageList();
    }
}
